using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Book.Blog.Core.DTOs;
using Book.Blog.Core.Interfaces;

namespace Book.Blog.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class StatisticsController : ControllerBase
    {
        private readonly IStatisticsService _statisticsService;

        public StatisticsController(IStatisticsService statisticsService)
        {
            _statisticsService = statisticsService;
        }

        /// <summary>
        /// 记录页面访问
        /// </summary>
        /// <param name="pageView">页面访问信息</param>
        /// <returns>操作结果</returns>
        /// <remarks>
        /// 示例请求:
        /// {
        ///   "url": "/posts/1",
        ///   "userId": "用户ID（可选）"
        /// }
        /// </remarks>
        [HttpPost("page-view")]
        public async Task<IActionResult> RecordPageView([FromBody] PageViewDTO pageView)
        {
            await _statisticsService.RecordPageViewAsync(pageView);
            return Ok();
        }

        /// <summary>
        /// 记录用户行为（需登录）
        /// </summary>
        /// <param name="userAction">用户行为信息</param>
        /// <returns>操作结果</returns>
        /// <remarks>
        /// 示例请求:
        /// {
        ///   "action": "like",
        ///   "targetId": "目标ID"
        /// }
        /// </remarks>
        [HttpPost("user-action")]
        [Authorize]
        public async Task<IActionResult> RecordUserAction([FromBody] UserActionDTO userAction)
        {
            await _statisticsService.RecordUserActionAsync(userAction);
            return Ok();
        }

        /// <summary>
        /// 获取统计总览（需管理员）
        /// </summary>
        /// <param name="request">统计请求参数</param>
        /// <returns>统计总览</returns>
        [HttpGet("overview")]
        [Authorize(Roles = "Admin")]
        public async Task<ActionResult<StatisticsOverviewDTO>> GetOverview([FromQuery] StatisticsRequestDTO request)
        {
            var overview = await _statisticsService.GetOverviewAsync(request);
            return Ok(overview);
        }

        /// <summary>
        /// 获取每日统计（需管理员）
        /// </summary>
        /// <param name="startDate">起始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>每日统计数据</returns>
        [HttpGet("daily")]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> GetDailyStatistics(
            [FromQuery] DateTime startDate,
            [FromQuery] DateTime endDate)
        {
            var stats = await _statisticsService.GetDailyStatisticsAsync(startDate, endDate);
            return Ok(stats);
        }

        /// <summary>
        /// 获取热门文章
        /// </summary>
        /// <param name="startDate">起始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="top">返回数量</param>
        /// <returns>热门文章列表</returns>
        [HttpGet("top-posts")]
        public async Task<IActionResult> GetTopPosts(
            [FromQuery] DateTime? startDate,
            [FromQuery] DateTime? endDate,
            [FromQuery] int top = 10)
        {
            var posts = await _statisticsService.GetTopPostsAsync(startDate, endDate, top);
            return Ok(posts);
        }

        /// <summary>
        /// 获取热门分类
        /// </summary>
        /// <param name="startDate">起始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="top">返回数量</param>
        /// <returns>热门分类列表</returns>
        [HttpGet("top-categories")]
        public async Task<IActionResult> GetTopCategories(
            [FromQuery] DateTime? startDate,
            [FromQuery] DateTime? endDate,
            [FromQuery] int top = 10)
        {
            var categories = await _statisticsService.GetTopCategoriesAsync(startDate, endDate, top);
            return Ok(categories);
        }

        /// <summary>
        /// 获取热门标签
        /// </summary>
        /// <param name="startDate">起始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="top">返回数量</param>
        /// <returns>热门标签列表</returns>
        [HttpGet("top-tags")]
        public async Task<IActionResult> GetTopTags(
            [FromQuery] DateTime? startDate,
            [FromQuery] DateTime? endDate,
            [FromQuery] int top = 10)
        {
            var tags = await _statisticsService.GetTopTagsAsync(startDate, endDate, top);
            return Ok(tags);
        }

        /// <summary>
        /// 生成每日统计（需管理员）
        /// </summary>
        /// <param name="date">日期（可选，默认前一天）</param>
        /// <returns>操作结果</returns>
        [HttpPost("generate-daily")]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> GenerateDailyStatistics([FromQuery] DateTime? date)
        {
            await _statisticsService.GenerateDailyStatisticsAsync(date ?? DateTime.UtcNow.AddDays(-1));
            return Ok();
        }

        /// <summary>
        /// 清理旧数据（需管理员）
        /// </summary>
        /// <param name="before">清理该日期之前的数据</param>
        /// <returns>操作结果</returns>
        [HttpPost("cleanup")]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> CleanupOldData([FromQuery] DateTime before)
        {
            await _statisticsService.CleanupOldDataAsync(before);
            return Ok();
        }
    }
} 